<template>
  <div class="app-container" style="width: 800px;margin: 30px auto" >
    <div id="printMe">
    <h1 style="text-align: center;">二便障碍患者智能评估结果</h1>
    <h3>基本信息</h3>
    <el-form label-width="100px" :model="info" ref="infoForm" >
      <el-row>
        <el-col :span="12">
          <el-form-item label="病例号：">
            <el-input v-model="info.medical_record_num" class="all" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日期：">
            <el-input v-model="info.date" class="all" readonly />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="姓名：">
            <el-input v-model="info.user_name" class="all" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="性别：">
            <el-input v-model="info.gender"  class="all" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="年龄：">
            <el-input v-model="info.age" class="all" readonly />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
        <el-form-item label="身高：">
          <el-input v-model="info.height" class="all" readonly />
        </el-form-item>
        </el-col>
        <el-col :span="8">
        <el-form-item label="体重：">
          <el-input v-model="info.weight" class="all" readonly />
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="主诉及病史：" >
            <el-input type="textarea" :autosize="{ minRows: 3 }" v-model="info.hpi" class="all" />
          </el-form-item>
        </el-col>
      </el-row>
      <h3>评估结果</h3>
      <el-row>
        <el-col>
          <el-form-item label="病情评估描述：" label-width="140px">
            <el-input type="textarea" :autosize="{ minRows: 3 }" v-model="info.remark" class="all" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="病情严重程度：" label-width="140px">
            <el-input type="textarea" :autosize="{ minRows: 2 }" v-model="info.result" class="all"  readonly />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="prescription">
        <el-col>
          <el-form-item label="处方：" label-width="140px">
            <el-input type="textarea" :autosize="{ minRows: 2 }" v-model="info.prescription" class="all"  readonly />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="zsk-div"  >
      <el-button type="primary" @click="go1">文献、指南和共识<br/>处方知识库</el-button>
      <el-button type="primary" @click="go2">磁刺激处方知识库</el-button>
      <el-button type="primary" @click="go3">电刺激处方知识库</el-button>
    </div>
    <el-row style="float: right;margin-right: 20%;">
      <el-button type="success" @click="openPrescription()">处方</el-button>
      <el-button type="danger" :disabled="!prescription" v-print="printObj">打印</el-button>
    </el-row>
  </div>

  <el-dialog v-model="dialogOpen" title="尿失禁-处方" destroy-on-close >
    <NsjPrescription :p-data="pData1" @comfirm="comfirmPrescription" @close="closePrescription"/>
  </el-dialog>
</template>

<script lang="ts" setup>
import { getCacheInfo, cacheInfo } from '@/utils/auth'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import useUserStore from '/src/store/modules/user'
import {useRouter} from "vue-router";
import { dayjs, ElMessage, ElMessageBox } from 'element-plus'
import NsjPrescription from './prescription-2'
import {getPrescriptione} from '@/api/login'


const userStore = useUserStore()
const router = useRouter()

const ii = getCacheInfo()
const info = reactive(ii)
// const infoForm = ref<FormInstance>()
const prescription = ref(false)
const dialogOpen = ref(false)

const create = () => {
  if (!info.medical_record_num){
    ElMessageBox.confirm('还未填写基本信息，请先完成','提醒',
        {
          confirmButtonText: '去填写',
          showCancelButton	: false,
          type: 'warning',
        })
        .then(() => {
          router.push({ path: "/index" });
        })
        .catch(() => {
        })
    return
  }
  if (!info.disorders_type){
    ElMessageBox.confirm('还未填写病史信息，请先完成','提醒',
        {
          confirmButtonText: '去填写',
          showCancelButton	: false,
          type: 'warning',
        })
        .then(() => {
          router.push({ path: "/step2" });
        })
        .catch(() => {
        })
    return
  }
  if (!info.testRusult){
    ElMessageBox.confirm('还未进行测试，请先完成','提醒',
        {
          confirmButtonText: '去测试',
          showCancelButton	: false,
          type: 'warning',
        })
        .then(() => {
          router.push({ path: "/test" });
        })
        .catch(() => {
          // catch error
        })
    return
  }
  info.date = dayjs(new Date()).format("YYYY-MM-DD")
  console.log("info",info)
  const r = []
  info.testRusult.forEach(tr => {
    r.push(tr.result)
  })
  info.result = r.join()
}

let pData1 = {}
const openPrescription = async (formEl: FormInstance | undefined) => {
  let nsjfs = 0
  info.testRusult.forEach(tr => {
    if (tr.type == '6'){
      nsjfs = tr.score
    }
  })
  getPrescriptione({score:nsjfs}).then( res => {
    pData1 = res
  })
  dialogOpen.value = true
}

const comfirmPrescription = (data) => {
  console.log("comfirmPrescription",data)
  pData1 = data
  info.prescription = 'Kegel模板训练: \r\t 强度：' + data.kegelqd + 'μV,治疗时间:' + data.kegelzlsj + ' min,收缩时间: ' + data.kegelsssj + 's,治疗天数: ' + data.kegelzlts + ' \n' +
      '电刺激: \r\t 电流强度: ' + data.dcjqd + 'μV,治疗时间: ' + data.dcjzlsj + 'min,收缩时间: ' + data.dcjpl + 's'
  dialogOpen.value = false
  prescription.value = true
}

const closePrescription = () => {
  dialogOpen.value = false
}

const printObj = {
  id: "printMe",
  url: null,
  popTitle:'&nbsp;',//页眉标题 默认浏览器标题 空字符串时显示undefined 使用html语言
  extraHead:'',//头部文字 默认空

}

const go1 = () => {
  router.push({ path: "/knowledge1" });
}
const go2 = () => {
  router.push({ path: "/knowledge2" });
}
const go3 = () => {
  router.push({ path: "/knowledge3" });
}

create()
</script>

<style scoped>
.all {
  width: 100%;
}
@media print {
  #printMe{
    display:block;
    height: auto;
    overflow: hidden;
  }
}

.zsk-div {
  position: absolute;
  top: 50px;
  right: 120px;
  width: 100px;
  display: inline-block;
}
.zsk-div button {
  margin-bottom: 10px;
  margin-left: 12px;
  font-size: 16px;
  padding: 22px;
}
</style>
